/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.controllers;

import aso.upna.cancion.*;
import aso.upna.playlist.GenericIterator;
import aso.upna.playlist.GenericList;
import java.io.File;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author triki
 */

public class SearchTableController {

    private JTable table;
    private GenericList lista;
    private String search_pattern;

    private final int DEF_ROWS = 13;


    public SearchTableController(JTable jTable)
    {
        table = jTable;
        lista = new GenericList();
        search_pattern = new String();
    }

    public void setSearchPattern(String sp)
    {
        this.search_pattern = new String(sp);
    }

    public void registerList(GenericList list)
    {
        lista = list;
    }
           
    public void refresh()
    {
        int rows,i;
       
        GenericIterator iter = null;
        File aux = null;
        String sAux = null;
        AbstractCancion can = null;
        Object obj[] = null;

        DefaultTableModel tab = (DefaultTableModel) table.getModel();       

        cleanTable();

        rows = table.getRowCount();
     
        if(lista == null)
            return;

        iter = lista.getIterator();
        i=0;
        
        while(iter.hasNext())
        {
            if(i > rows-1)
            {
                tab.addRow(obj);
            }

            can = (AbstractCancion) iter.current();
            sAux = can.getFilename();
            aux = new File(can.getPath()+sAux);
            
            if(can.is_local())
            {
                table.setValueAt("Local",i, 0);
                table.setValueAt(" - ",i, 1);
            }
            else
            {
                table.setValueAt("Remota",i, 0);
                table.setValueAt(""+can.getPath(),i, 1);
            }

            table.setValueAt(can.getFilename(),i, 2);

            if(can.hasMetadata())
            {
                table.setValueAt(can.getMetadatos().getTitulo(),i, 3);
                table.setValueAt(can.getMetadatos().getGrupo(),i,4);
            }
            table.setValueAt(""+can.getDuracion(), i, 5);
            table.setValueAt(""+convertToMb(can.getFilesize()), i, 6);
            i++;
            
            iter.next();
        }
    }

    public static GenericList searchByPattern(GenericList lista,String search_pattern)
    {
        GenericIterator it = lista.getIterator();
        GenericList lis = new GenericList();
        CharSequence ch_aux;
        AbstractCancion c_aux;
        String s_aux;

        search_pattern = search_pattern.toLowerCase();

        while(it.hasNext())
        {           
            c_aux = (AbstractCancion)it.current();
            it.next();
            ch_aux = search_pattern.subSequence(0,search_pattern.length());

            //Buscamos en el nombre del archivo.
            s_aux = c_aux.getFilename();
            s_aux = s_aux.toLowerCase();
            
            if(s_aux.contains(ch_aux))
            {
                lis.addElement(c_aux);
                continue;
            }

            if(!c_aux.hasMetadata())
                continue;

            if(c_aux.getMetadatos().getTitulo().contains(ch_aux))
            {
                lis.addElement(c_aux);
                continue;
            }

            if(c_aux.getMetadatos().getGrupo().contains(ch_aux))
            {
                lis.addElement(c_aux);
                continue;
            }

            if(c_aux.getMetadatos().getAlbum().contains(ch_aux))
            {
                lis.addElement(c_aux);
                continue;
            }
        }
        
        return lis;
    }
   
    private void cleanTable()
    {
        int rows,cols,i,j;
        
        DefaultTableModel tab = (DefaultTableModel) table.getModel();
        rows = table.getRowCount();
        cols = table.getColumnCount();

        tab.setNumRows(DEF_ROWS);
        
        rows = table.getRowCount();

        for(i=0;i<rows;i++)
        {
            for(j=0;j<cols;j++)
            {
                table.setValueAt("", i, j);
            }
        }
    }

    public GenericList getSelectedRows()
    {
        int num_rows[],j;
        GenericList list = new GenericList();

        num_rows = table.getSelectedRows();

        for(j=0;j<num_rows.length;j++)
        {
            list.addElement(lista.getElementAt(num_rows[j]));
        }

        return list;
    }
        
    private static String convertToMb(long bytes)
    {
        if(bytes == 0)
            return "0 Mb";
        
        float mb = (float)bytes/(1024*1024);
        String aux = new String(""+mb);
        int pos = aux.indexOf(".");

        return ""+aux.substring(0, pos+3)+" Mb";
    }
}
